package com.ease.platform.access.service.impl;

import java.io.Serializable;
import java.util.List;

import org.springframework.jdbc.core.RowMapper;


import com.ease.common.vo.Ordering;
import com.ease.common.vo.Pagination;
import com.ease.dao.DataAccessObjectConst;
import com.ease.dao.jdbctemplate.rowmapper.ParameterizedBeanPropertyRowMapper;
import com.ease.dao.statement.util.NamedStatementUtil;
import com.ease.model.criteria.CriteriaParameter;
import com.ease.platform.access.service.AbstractParameterizedBeanJdbcTemplateAccessService;

/**
 *
 * @author nathanleewei
 *
 */
public abstract class AbstractParameterizedBeanJdbcTemplateAccessServiceImpl<T extends Serializable> extends AbstractParameterizedBeanAccessServiceImpl<T> implements
		AbstractParameterizedBeanJdbcTemplateAccessService<T> {

	protected void initNamedStatementFactory() {
		initNamedStatementFactory(parameterizedBeanClass);
	}

	protected List<T> find(String statementName, Object parameter, RowMapper<T> rowMapper) {
		String statement = getNamedStatement(statementName, parameter);

		if (null != parameter) {
			return this.getJdbcTemplateDataAccessObject().find(statement, parameter, rowMapper);
		}
		return this.getJdbcTemplateDataAccessObject().find(statement, rowMapper);
	}

	public List<T> findAll() {
		return findAll(null);
	}

	public List<T> findAll(Ordering ordering) {
		String name = NamedStatementUtil.statementName(namespace, DataAccessObjectConst.STATEMENT_FIND_ALL);
		String namedStatement = getNamedStatement(name, null);
		// TODO String namedStatement = getNamedStatement(name, ordering);
		return this.getJdbcTemplateDataAccessObject().find(namedStatement, ordering, this.getRowMapper());
	}

	public List<T> find(CriteriaParameter criteriaParameter) {
		String name = NamedStatementUtil.statementName(namespace, DataAccessObjectConst.STATEMENT_FIND);		
		return find(name, criteriaParameter, this.getRowMapper());
	}

	public Pagination<T> findPagination(CriteriaParameter criteriaParameter, Pagination<T> pagination) {
		return this.findPagination(criteriaParameter, null, pagination);
	}

	public Pagination<T> findPagination(CriteriaParameter criteriaParameter, Ordering ordering, Pagination<T> pagination) {
		String name = NamedStatementUtil.statementName(namespace, DataAccessObjectConst.STATEMENT_FIND);
		String namedStatement = getNamedStatement(name, criteriaParameter);
		return this.getJdbcTemplateDataAccessObject().findPagination(namedStatement, criteriaParameter, ordering, pagination, this.getRowMapper());
	}

	public RowMapper<T> getRowMapper() {
		return (RowMapper<T>) ParameterizedBeanPropertyRowMapper.newInstance(parameterizedBeanClass);
	}

};